﻿
using SvGame.Logic;

namespace SvGame.Controller;

public partial class GameHandler
{
    /// <summary>
    /// 道具
    /// </summary>
    /// <param name="pack"></param>
    public int C2GItems(MyPackage pack)
    {
        pack.GetMsg(out C2GItems req);
        var rsp = new G2CItems();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        rsp.InfoItems = player.InfoItems();

        return pack.Send(rsp);
    }
    /// <summary>
    /// 道具拾取
    /// </summary>
    /// <param name="pack"></param>
    public int C2GItemGet(MyPackage pack)
    {
        pack.GetMsg(out C2GItemGet req);
        var rsp = new G2CItemGet();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }
        if (!player.Room.DictItem.TryGetValue(req.Uid, out var obj))
        {
            return player.MessageError("没有这个道具");
        }
        if (!obj.CanGet)
        {
            return player.MessageError("这个东西拿不起来。");
        }
        player.ItemGetFromRoom(obj);

        MessageVision($"$N捡起{obj.DescShort()}。", player);

        player.Room.BroadcastInfo();
        player.NtfPlayerItems();

        return pack.Send(rsp);
    }
    /// <summary>
    /// 道具丢弃
    /// </summary>
    /// <param name="pack"></param>
    public int C2GItemDrop(MyPackage pack)
    {
        pack.GetMsg(out C2GItemDrop req);
        var rsp = new G2CItemDrop();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }
        if (!player.DictItem.TryGetValue(req.Uid, out var obj))
        {
            return player.MessageError("没有这个道具");
        }
        if (!ConfItem.TryGetValue(obj.ConfigId, out var conf))
        {
            return player.MessageError("配置ID错误");
        }
        player.ItemDropToRoom(obj, req.Count);

        MessageVision($"$N丢下{Efun.ChineseNumber(req.Count)}{conf.Unit}{conf.Name}。", player);

        player.Room.BroadcastInfo();
        player.NtfPlayerItems();

        return pack.Send(rsp);
    }

    /// <summary>
    /// 道具使用
    /// </summary>
    /// <param name="pack"></param>
    public int C2GItemUse(MyPackage pack)
    {
        pack.GetMsg(out C2GItemUse req);
        var rsp = new G2CItemUse();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }
        if (!player.DictItem.TryGetValue(req.Uid, out var obj))
        {
            return player.MessageError("没有这个道具");
        }
        var err = player.ItemUse(obj);
        if (!err.IsNullOrEmpty())
        {
            return player.MessageError(err);
        }

        player.NtfPlayerItems();

        return pack.Send(rsp);
    }

    /// <summary>
    /// 道具动作
    /// </summary>
    /// <param name="pack"></param>
    public int C2GItemAct(MyPackage pack)
    {
        pack.GetMsg(out C2GItemAct req);
        var rsp = new G2CItemAct();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }
        if (!player.DictItem.TryGetValue(req.Uid, out var obj))
        {
            return player.MessageError("没有这个道具");
        }
        var err = player.ItemAct(obj, (EActType)req.Act);
        if (!err.IsNullOrEmpty())
        {
            return player.MessageError(err);
        }

        player.NtfPlayerItems();

        return pack.Send(rsp);
    }
}
